package com.itheima.appserver.controller;

import cn.hutool.core.map.MapUtil;
import com.itheima.appserver.service.MyCenterService;
import com.itheima.commons.model.vo.CountsVo;
import com.itheima.commons.model.vo.PageResult;
import com.itheima.commons.model.vo.SettingsVo;
import com.itheima.commons.model.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 个人中心相关API接口
 */
@RequestMapping("users")
@RestController
public class MyCenterController {

    @Autowired
    private MyCenterService myCenterService;

    /**
     * 根据用户id查询用户信息
     *
     * @param userId 用户id，如果为空，表示查询当前登录人的信息
     * @return
     */
    @GetMapping
    public ResponseEntity queryUserInfoByUserId(@RequestParam(value = "userID", required = false) Long userId) {
        UserInfoVo userInfoVo = myCenterService.queryUserInfoByUserId(userId);
        return ResponseEntity.ok(userInfoVo);
    }

    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     *
     * @return
     */
    @GetMapping("counts")
    public ResponseEntity queryCounts() {
        CountsVo countsVo = myCenterService.queryCounts();
        return ResponseEntity.ok(countsVo);
    }

    /**
     * 互相关注、我关注、粉丝、谁看过我 - 翻页列表
     *
     * @param type     1 互相关注 2 我喜欢 3 粉丝 4 谁看过我
     * @param page
     * @param pageSize
     * @param nickname
     * @return
     */
    @GetMapping("friends/{type}")
    public ResponseEntity queryLikeList(@PathVariable("type") String type,
                                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                                        @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                        @RequestParam(value = "nickname", required = false) String nickname) {
        page = 1;
        PageResult pageResult = myCenterService.queryLikeList(Integer.valueOf(type), page, pageSize, nickname);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 取消喜欢
     *
     * @param userId
     * @return
     */
    @DeleteMapping("like/{uid}")
    public ResponseEntity disLike(@PathVariable("uid") Long userId) {
        myCenterService.disLike(userId);
        return ResponseEntity.ok(null);
    }

    /**
     * 关注粉丝
     *
     * @param userId
     * @return
     */
    @PostMapping("fans/{uid}")
    public ResponseEntity likeFan(@PathVariable("uid") Long userId) {
        myCenterService.likeFan(userId);
        return ResponseEntity.ok(null);
    }

    /**
     * 更新用户信息
     *
     * @param userInfoVo
     * @return
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfoVo userInfoVo) {
        myCenterService.updateUserInfo(userInfoVo);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询配置
     *
     * @return
     */
    @GetMapping("settings")
    public ResponseEntity querySettings() {
        SettingsVo settingsVo = myCenterService.querySettings();
        return ResponseEntity.ok(settingsVo);
    }

    /**
     * 设置陌生人问题
     *
     * @return
     */
    @PostMapping("questions")
    public ResponseEntity saveQuestions(@RequestBody Map<String, String> param) {
        String content = param.get("content");
        myCenterService.saveQuestions(content);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询黑名单
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("blacklist")
    public ResponseEntity queryBlacklist(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                         @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize) {
        PageResult pageResult = myCenterService.queryBlacklist(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 移除黑名单
     *
     * @return
     */
    @DeleteMapping("blacklist/{uid}")
    public ResponseEntity delBlacklist(@PathVariable("uid") Long userId) {
        myCenterService.delBlacklist(userId);
        return ResponseEntity.ok(null);
    }

    /**
     * 更新通知设置
     *
     * @param param
     * @return
     */
    @PostMapping("notifications/setting")
    public ResponseEntity updateNotification(@RequestBody Map<String, Boolean> param) {
        Boolean likeNotification = param.get("likeNotification");
        Boolean pinglunNotification = param.get("pinglunNotification");
        Boolean gonggaoNotification = param.get("gonggaoNotification");

        myCenterService.updateNotification(likeNotification, pinglunNotification, gonggaoNotification);
        return ResponseEntity.ok(null);
    }

    /**
     * 上传头像
     *
     * @param file
     * @return
     */
    @PostMapping("header")
    public ResponseEntity saveLogo(@RequestParam("headPhoto") MultipartFile file) {
        myCenterService.saveUserLogo(file);
        return ResponseEntity.ok(null);
    }

    /**
     * 发送短信验证码
     *
     * @return
     */
    @PostMapping("phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        myCenterService.sendVerificationCode();
        return ResponseEntity.ok(null);
    }

    /**
     * 校验验证码
     *
     * @param param
     * @return
     */
    @PostMapping("phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map<String, String> param) {
        Boolean bool = myCenterService.checkVerificationCode(param.get("verificationCode"));
        return ResponseEntity.ok(MapUtil.builder("verification", bool).build());
    }

    /**
     * 保存新手机号
     *
     * @return
     */
    @PostMapping("phone")
    public ResponseEntity updatePhone(@RequestBody Map<String, String> param) {
        String newPhone = param.get("phone");
        myCenterService.updatePhone(newPhone);
        return ResponseEntity.ok(null);
    }
}